﻿@{ 
    var m = "Job";//定义全局变量 单另一个功能复制此代码时 不需要修改太多代码
}
<div id="dg@(m)Toolbar" class="datagrid-toolbar1">
    <a class="easyui-linkbutton" iconCls="icon-add" plain="true">@L("Add")</a>
    <a class="easyui-linkbutton" iconCls="icon-edit" plain="true">@L("Update")</a>
    <a class="easyui-linkbutton" iconCls="icon-remove" plain="true">@L("Delete")</a>
    <a class="easyui-linkbutton" iconCls="icon-reload" plain="true">@L("Refresh")</a>
</div>
<table id="dg@(m)"></table>

<script>
    (function () {
        //若在js函数内部直接使用 razor变量，将失去vs2017提供的js代码折叠效果，因此这里搞个变量
        var m = '@m';
    
        var canAdd = abp.auth.isGranted('@ZLJ.Authorization.PermissionNames.AdministratorBaseInfoJobCreate'); 
        var canUpdate =  abp.auth.isGranted('@ZLJ.Authorization.PermissionNames.AdministratorBaseInfoJobUpdate'); 
        var canDelete =  abp.auth.isGranted('@ZLJ.Authorization.PermissionNames.AdministratorBaseInfoJobDelete'); 

        var $btns = $('#dg'+m+'Toolbar a');
        var $dg = $('#dg'+m);
        var $cmenu = $('<div />');

        $cmenu.menu();
        if(canAdd){
            $cmenu.menu('appendItem', {
                text: $btns.eq(0).text(),
                iconCls: $btns.eq(0).attr('iconCls'),
                onclick: function () {
                    $btns.eq(0).trigger('click');
                }
            });
        } else
            $btns.eq(0).hide();
        if(canUpdate){
            $cmenu.menu('appendItem', {
                text: $btns.eq(1).text(),
                iconCls: $btns.eq(1).attr('iconCls'),
                onclick: function () {
                    $btns.eq(1).trigger('click');
                }
            });
        }else
            $btns.eq(1).hide();
        if(canDelete){
            $cmenu.menu('appendItem', {
                text: $btns.eq(2).text(),
                iconCls: $btns.eq(2).attr('iconCls'),
                onclick: function () {
                    $btns.eq(2).trigger('click');
                }
            });
        }else
            $btns.eq(2).hide();

        //str:删除 审核，注意本地化
        function getSelected(str) {
            var rows = $dg.datagrid('getSelections');
            if (rows.length == 0) {//若没有选择，返回的是一个空数组，而非undifined/null
                abp.notify.warn(abp.localization.abp('PleaseSeleteLines', $.trim(str)));
                return false;
            }
            return rows;
        }
        //只有新增和修改会使用编辑表单页面，批量审核或其它需要表单的操作应该另外定义方法而不是高度抽象一个表单窗口
        //em：编辑时的id 若不传则新增
        function showForm(id) {
            var $dlg = $('<div id="dlg'+m+'" />');
            if (id)
                $dlg.data('id', id);
            
            $dlg.data('onSaved', function () {
                $dlg.dialog('close');
                $dg.datagrid('reload');
            });
            
            $dlg.dialog({
                iconCls: id ? $btns.eq(1).attr('iconCls') : $btns.eq(0).attr('iconCls'),
                title: (id ? $btns.eq(1).text() : $btns.eq(0).text()).trim() + abp.localization.abp(m),
                width: 500,
                height: 200,
                border: false,
                modal: true,
                href: abp.appPath + 'AbpAppView/Load?viewUrl=/views/baseinfo/'+m+'/form.cshtml&_t=' + abp.pageLoadTime.getTime(),
                buttons: [{
                    iconCls: 'icon-ok',
                    text: abp.localization.abp('Save'),
                    handler: function () {
                        var callback = $dlg.data('clickSave');
                        if (callback)
                            callback();
                    }
                }, {
                    iconCls: 'icon-undo',
                        text: abp.localization.abp('Back'),
                    handler: function () {
                        $dlg.dialog('close');
                    }
                }],
                onClose: function () {
                    $dlg.dialog('destroy');
                }
                //可以通过onload事件初始化控件，如：下拉框
            });
        }

        $btns.eq(0).click(function () {
            showForm();
        });
        $btns.eq(1).click(function () {
            var rows = getSelected($(this).text());
            if (!rows)
                return false;

            showForm(rows[0].id);
        });
        $btns.eq(2).click(function () {
            var rows = getSelected(abp.localization.abp('Delete'));
            if (!rows)
                return false;

            abp.message.confirm(
                '确认删除选中的行吗？',
                '删除确认',
                function (isConfirmed) {
                    if (isConfirmed) {
                        var ary = new Array();
                        $(rows).each(function () {
                            ary.push(this.id);
                        });
                        abp.services.app.job.deleteBatch(ary).done(function (d) {
                            abp.notify.success(abp.localization.abp("Delete") + abp.localization.abp("Success") + '!');
                            $btns.eq(3).trigger('click');
                        });
                    }
                }
            );
        });
        $btns.eq(3).click(function () {
            $dg.datagrid('reload');
        });


        $dg.datagrid({
            ctrlSelect: true,
            fit: true,
            pagination: false,
            checkOnSelect:true,
            selectOnCheck:true,
            singleSelect:false,
            pageSize1: 20,
            border: false,
            showFooter1: true,
            rownumbers:true,
            toolbar: '#dg'+m+'Toolbar',
            url: abp.appPath + 'api/services/app/'+m+'/GetAllList',
            columns: [[
                { field: 'name', title: '岗位名称', width: 300 }
            ]],
            frozenColumns: [[
                { field: 'ck', checkbox: true }
                //{
                //    field: 'opt', title: '操作', width: 80, align: 'center', formatter: function (val, row, index) {
                //        var ct = '<a no="' + row.id + '" permission="' + editPermission + '" class="btnEdit' + m + ' easyui-linkbutton" data-options="plain:true,iconCls:\'icon-edit\'"></a>';
                //        ct += '<a no="' + row.id + '" permission="' + delPermission + '" class="btnDel' + m + ' easyui-linkbutton" data-options="plain:true,iconCls:\'icon-remove\'"></a>';
                //        return ct;
                //    }
                //},
                //{ field: 'name', title: '名称', width: 150 }
            ]],
            onLoadSuccess: function (data) {

                /*$.parser.parse($dg.parent());
                var $btnEdits = $('.btnEdit' + m);
                app.permission.autoHideOrShow($btnEdits);

                var $btnDels = $('.btnDel' + m);
                app.permission.autoHideOrShow($btnDels);

                $btnEdits.click(function () {
                    showForm($(this).attr('no'));
                });

                $btnDels.click(function () {
                    var that = this;
                    abp.message.confirm(
                        '确认删除选中的行吗？',
                        '删除确认',
                        function (isConfirmed) {
                            if (isConfirmed) {
                                abp.services.app.role.delete({ id: $(that).attr('no') }).then(function () {
                                    abp.notify.success(abp.localization.abp('DeleteSuccess'));
                                    $dg.datagrid('reload');
                                });
                            }
                        }
                    );

                });*/
            },
            onLoadError: function () {
                abp.notify.error('加载数据失败！请稍候重试。');
            },
            onRowContextMenu:function(e,index,row){
                e.preventDefault();//禁止浏览器默认右键菜单
                var rows = $dg.datagrid('getSelections');
                if (rows.length == 1)
                    $dg.datagrid('clearSelections')
                $dg.datagrid('selectRow',index);
                $cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onDblClickRow: function (index, row) {
                $btns.eq(1).trigger('click');
            }
        });
     }());
</script>